
#include "object/face.h"

Face::Face(const QVector< const QVector3D* >& rkVertices,
           const QVector< const QVector3D* >& rkNormals,
           const QVector< const QVector2D* >& rkUVs,
           Material* pkMaterial)
    : m_bIsValid(false),
      m_kVertices(rkVertices),
      m_kNormals(rkNormals),
      m_kUVs(rkUVs),
      m_pkMaterial(pkMaterial)
{
    // Check for validity.
    if((m_kUVs.count() != 0 && m_kUVs.count() != m_kVertices.count()) ||
       (m_kNormals.count() != 0 && m_kNormals.count() != m_kVertices.count()))
        return;

    m_bIsValid = true;
}

Face::~Face()
{
}

bool Face::isValid() const
{
    return m_bIsValid;
}

const QVector< const QVector3D* >& Face::vertices() const
{
    return m_kVertices;
}

const QVector< const QVector3D* >& Face::normals() const
{
    return m_kNormals;
}

const QVector< const QVector2D* >& Face::UVs() const
{
    return m_kUVs;
}

Material* Face::material() const
{
    return m_pkMaterial;
}

QVector3D Face::barycenter() const
{
    QVector3D kSum;

    foreach(const QVector3D* pkVertex, m_kVertices)
        kSum += *pkVertex;

    return kSum / m_kVertices.count();
}
